(function() {

this.pasta = this.pasta || {};
this.pasta.webgui = this.pasta.webgui || {};

this.pasta.webgui._systemCurrCID = 0;
this.pasta.webgui._controlList = [];

this.pasta.webgui.Control = Class.extend(
{
	getNextCID : function()
	{
		pasta.webgui._systemCurrCID++;
		return pasta.webgui._systemCurrCID;
	},
	
	init : function() 
	{
		this.cid = "obj" + this.getNextCID();
		
		this.left = 0;
		this.top = 0;
		this.width = 32;
		this.height = 32;
		this.visible = true;
		
		this.canvas = document.createElement("div");
		
		this.canvas.id = this.cid;
		this.canvas.style.position = "absolute";
		this.canvas.style.left = this.left + "px";
		this.canvas.style.top = this.top + "px";
		this.canvas.style.width = this.width + "px";
		this.canvas.style.height = this.height + "px";
		
		this.parentId = null;
		pasta.webgui._controlList[this.cid] = this;
		
		this.bgColor = -1;
		this.borderWeight = 1;
		this.borderColor = null;
		
		this._onClick = null;
		this._onMouseOver = null;
		this._onMouseOut = null;
		this._onMouseUp = null;
		this._onMouseUp = null;
		this._onMouseDown = null;
	},
	
	free : function()
	{
		if (this.getParent() != null)
			this.getParent().delControl(this);
	},
	
	identify : function()
	{
		alert(this.cid);
	},
	
	say : function(msg)
	{
		alert("Msg : " + msg);
	},
	
	getParent : function()
	{
		return pasta.webgui._controlList[this.parentId];
	},
	
	bringToFront : function()
	{
		var parentObj = this.getParent();
		
		if (parentObj != null)
			parentObj.bringChildToFront(this);
	},
	
	sendToBack : function()
	{
		var parentObj = this.getParent();
		
		if (parentObj != null)
			parentObj.sendChildToBack(this);
	},
	
	setButtonMode : function(data)
	{
		if (data)
		{
			this.canvas.style.cursor = "pointer";
			
			var obj = this;
			
			this.canvas.onclick = function(e) { if (!e) e = window.event; obj.doClick(); };
			this.canvas.ondblclick = function(e) { if (!e) e = window.event; obj.doDblClick(); };
			this.canvas.onmouseover = function(e) { if (!e) e = window.event; obj.doMouseOver(); };
			this.canvas.onmouseout = function(e) { if (!e) e = window.event; obj.doMouseOut(); };
			this.canvas.onmousedown = function(e) { if (!e) e = window.event; obj.doMouseDown(e.offsetX, e.offsetY); };
			this.canvas.onmouseup = function(e) { if (!e) e = window.event; obj.doMouseUp(e.offsetX, e.offsetY); };
			this.canvas.ontouchstart = function(e) { if (!e) e = window.event; obj.doMouseDown(e.offsetX, e.offsetY); };
			this.canvas.ontouchend = function(e) { if (!e) e = window.event; obj.doMouseUp(e.offsetX, e.offsetY); };
		}
		else
		{
			this.canvas.style.cursor = "auto";
			
			this.canvas.onclick = undefined;
			this.canvas.onmouseover = undefined;
			this.canvas.onmouseout = undefined;
			this.canvas.onmousedown = undefined;
			this.canvas.onmouseup = undefined;
			this.canvas.ontouchstart = undefined;
			this.canvas.ontouchend = undefined;
		}
	},
	
	getClientRect : function()
	{
		return this.canvas.getBoundingClientRect();
	},
	
	//---------------------- Event Handler ----------------------
	
	doAddedToStage : function()
	{
	},
	
	doDblClick : function()
	{
		if (this._onDblClick != null)
			this._onDblClick.callHandler(this);
	},
	
	doClick : function()
	{
		if (this._onClick != null)
			this._onClick.callHandler(this);
	},
	
	doMouseOver : function()
	{
		if (this._onMouseOver != null)
			this._onMouseOver.callHandler(this);
	},
	
	doMouseOut : function()
	{
		if (this._onMouseOut != null)
			this._onMouseOut.callHandler(this);
	},
	
	doMouseDown : function(x, y)
	{
		if (this._onMouseDown != null)
			this._onMouseDown.callHandler(this, x, y);
	},
	
	doMouseUp : function(x, y)
	{
		if (this._onMouseUp != null)
			this._onMouseUp.callHandler(this, x, y);
	},
	
	//--------------------- Setter & Getter -------------------
	
	getShadow : function()
	{
		return this.shadow;
	},
	
	setShadow : function(data)
	{
		this.shadow = data;
		
		this.canvas.style.webkitBoxShadow = data;
		this.canvas.style.mozBoxShadow = data;
		this.canvas.style.boxShadow = data;
	},
	
	getBgColor : function()
	{
		return this.bgColor;
	},
	
	setBgColor : function(data)
	{
		this.bgColor = data;
		
		if ((data == -1) || (data == null))
			this.canvas.style.background = "";
		else
			this.canvas.style.background = data;
	},
	
	getBorderColor : function()
	{
		return this.borderColor;
	},
	
	setBorderColor : function(data)
	{
		this.borderColor = data;
		
		this.initBorder();
	},
	
	getBorderWeight : function()
	{
		return this.borderWeight;
	},
	
	setBorderWeight : function(data)
	{
		this.borderWeight = data;
		
		this.initBorder();
	},
	
	initBorder : function()
	{
		if ((this.borderColor == null) || (this.borderWeight == null))
			this.canvas.style.border = "";
		else
			this.canvas.style.border = this.borderWeight + "px solid " + this.borderColor;
	},
	
	getCanvas : function()
	{
		return this.canvas;
	},
	
	getLeft : function()
	{
		return this.left;
	},
	
	setLeft : function(data)
	{
		this.left = data;
		this.canvas.style.left = this.left + "px";
	},
	
	getTop : function()
	{
		return this.top;
	},
	
	setTop : function(data)
	{
		this.top = data;
		this.canvas.style.top = this.top + "px";
	},
	
	getWidth : function()
	{
		return this.width;
	},
	
	setWidth : function(data)
	{
		this.width = data;
		this.canvas.style.width = this.width + "px";
	},
	
	getHeight : function()
	{
		return this.height;
	},
	
	setHeight : function(data)
	{
		this.height = data;
		this.canvas.style.height = this.height + "px";
	},
	
	isVisible : function()
	{
		return this.visible;
	},
	
	setVisible : function(data)
	{
		this.visible = data;
		this.canvas.style.display = (this.visible ? "" : "none");
	},
	
	getTooltip : function()
	{
		return this.canvas.title;
	},
	
	setTooltip : function(data)
	{
		this.canvas.title = data;
	},
	
	//--------------------------- Event -------------------------
	
	getOnClick : function()
	{
		if (this._onClick == null)
			this._onClick = new pasta.util.EventObj();
		
		return this._onClick;
	},
	
	getOnDblClick : function()
	{
		if (this._onDblClick == null)
			this._onDblClick = new pasta.util.EventObj();
		
		return this._onDblClick;
	},
	
	getOnMouseOver : function()
	{
		if (this._onMouseOver == null)
			this._onMouseOver = new pasta.util.EventObj();
		
		return this._onMouseOver;
	},
	
	getOnMouseOut : function()
	{
		if (this._onMouseOut == null)
			this._onMouseOut = new pasta.util.EventObj();
		
		return this._onMouseOut;
	},
	
	getOnMouseUp : function()
	{
		if (this._onMouseUp == null)
			this._onMouseUp = new pasta.util.EventObj();
		
		return this._onMouseUp;
	},
	
	getOnMouseDown : function()
	{
		if (this._onMouseDown == null)
			this._onMouseDown = new pasta.util.EventObj();
		
		return this._onMouseDown;
	}
});

})();